package com.shopping.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopping.service.ShopCarService;
import com.shopping.service.UserService;
import com.shopping.service.impl.ShopCarServiceImpl;
import com.shopping.service.impl.UserServiceImpl;


/**
 * @author zjh
 *
 */
@WebServlet("/delGoods.do")
public class DelGoodsServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		int goodsId = Integer.parseInt(req.getParameter("goodsId"));
		UserService us = new UserServiceImpl();
		ShopCarService scs = new ShopCarServiceImpl();
		int userId = 0;
		try {
			userId = us.getUserId(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int num = scs.deleteGoods(userId, goodsId);
		ObjectMapper om = new ObjectMapper();
		resp.setCharacterEncoding("UTF-8"); //设置 后端给前端传回的编码格式为utf-8
		resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		resp.getWriter().print(om.writeValueAsString(num));
	}
	
}
